package mrirnaproject;

import java.io.BufferedWriter; 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
/*import java.io.FileReader;*/
import java.util.Scanner;

import org.biojava3.core.sequence.DNASequence;
import org.biojava3.core.sequence.RNASequence;
import org.biojava3.core.sequence.compound.NucleotideCompound;
import org.biojava3.core.sequence.template.SequenceView;

public class InputFiles1 {



	public String RefFile() throws IOException {


		//prompt user to input the various files
		Scanner sc = new Scanner(System.in);

		//User puts in Reference gDNA file (.fasta)
		String subPattern = "";
		Scanner input = null;

		System.out.println("Input the filepath of the DNA reference file:\n");



		boolean isFile = false;


		while (isFile == false){    

			String fileName = sc.next();
			File inputFile = new File(fileName);

			//Checks if file exits
			//If yes, reads file
			if (inputFile.exists()){
				try {
					input = new Scanner(inputFile);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				isFile = true;
			
			
			//Parses header if applicable
			//char c = input.nextLine().charAt(0);


			while(input.hasNextLine()){
				if( input.nextLine().startsWith(">")) {
					input.nextLine();
					subPattern = subPattern + input.nextLine(); 
				}

				//throw new IOException("Remove header from " + inputFile );
				subPattern = subPattern + input.nextLine();
			}
		} input.close();
		}

		System.out.println("Subpattern: " + subPattern);

		//Converts file to DNA sequence
		//Converts DNA to RNA
		//Takes Reverse complement of RNA query
		DNASequence DNAfile = new DNASequence(subPattern);
		RNASequence RNAfile = DNAfile.getRNASequence();
		SequenceView<NucleotideCompound> compRNA = RNAfile.getReverseComplement();

		System.out.println(RNAfile.toString());
		System.out.println(compRNA.toString());

		/*end of processing the Ref gDNA file*/

		//User puts in file with the miRNA(.txt)
		/*System.out.println("Input path of miRNA file:");

		Scanner input_again = null;

		String miRNA = null;

		boolean isFile2 = false;

		//Checks if file exits
		//If yes, reads file
		while (isFile2 == false){    

			String fileName2 =sc.next();
			File inputFile2 = new File(fileName2);

			if (inputFile2.exists()){
				try {
					input_again = new Scanner(inputFile2);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				isFile2 = true;
			}
		}

		String result = "miRNAs.txt";
		FileWriter report = new FileWriter(result);
		BufferedWriter wreport = new BufferedWriter(report);

		while(input_again.hasNextLine()){
			miRNA = input_again.nextLine();
			myQ.add(miRNA);
		} 


		input_again.close();*/

		/* end of processing the miRNA file */

		//Prompts user to enter the scientic name of the organism the reference genome
		System.out.println("Please enter the scientific name of Reference Organism:");
		System.out.println(System.getProperty("user.dir"));

		String Orgname;
		String gName;
		String sName;
		Scanner input3= null;
		String result2 = "AnnotatedmiRNA.txt";
		gName = sc.next();
		sName = sc.next();
		Orgname = gName + " " + sName + ".fasta";  
	
		File Refan = new File(Orgname);

		//Checks if filename exists
		//If yes, reads file
		if (Refan.exists()){
			try {
				input3 = new Scanner(Refan);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			isFile = true;
		} 


		String subPattern2 = "";

		//reads only lines that don't belong to the header
		FileWriter report2 = new FileWriter(result2);
		BufferedWriter Annreport = new BufferedWriter(report2);
		while(input3.hasNextLine()){
			if(input3.nextLine().startsWith(">")){
				subPattern2 = input3.nextLine();
				Annreport.write(subPattern2);
				Annreport.newLine();
			}
			

		}
		Annreport.close();
		sc.close();
		input3.close();
		
		
		return result2;
		
		
	}
	}


